package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.InfDictSort;
import com.ctcemti.mdm.project.sys.entity.formdata.BaseReq;
import com.ctcemti.mdm.project.sys.service.InfDictSortService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author bxf
 * @Description 数据字典字典分类表控制器层
 * @Date 8:32 2019/7/18
 * @Param 
 * @return 
 **/
@RestController
@Api(value = "InfDictSortController", description = "数据字典字典分类表控制控制器类")
@RequestMapping("infdictsort")
public class InfDictSortController {
    @Autowired
    private InfDictSortService infDictSortService;

    @ApiOperation(value = "根据数据字典字典分类id或者名称获取子级(异步加载)")
    @PostMapping("selectInfdictsortByPid")
    @ApiImplicitParam(name="id",value = "父级id")
    public Result<List<InfDictSort>> selectByPid(HttpServletRequest request, HttpServletResponse response , String id, String name){
        List<InfDictSort> list = new ArrayList<InfDictSort>();
        if(StringUtil.isEmpty(id)){
            InfDictSort InfDictSort = new InfDictSort();
            InfDictSort.setId(-1);
            InfDictSort.setName("数字字典");
            InfDictSort.setParent(true);
            InfDictSort.setOpen(true);
            if(StringUtil.isEmpty(name)){
                list = infDictSortService.selectByPid("-1");
                list.add(InfDictSort);
            }else{
                list = infDictSortService.selectInfdictsortByInfdictsortName(name);
                list.add(InfDictSort);
            }

        }else{
            list = infDictSortService.selectByPid(id);
        }

        return new Result<List<InfDictSort>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "分页数据字典字典分类")
    @PostMapping("selectListByPid")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value = "父级id"),@ApiImplicitParam(name="infDictSort",value = "字典分类实体类"),@ApiImplicitParam(name="id",value = "父级id")})
    public Result<PageInfo<InfDictSort>> selectListByPid(HttpServletRequest request, HttpServletResponse response , String id, BaseReq baseReq){

        PageInfo<InfDictSort> pageInfo = infDictSortService.selectListByPid(id,baseReq);
        return new Result<PageInfo<InfDictSort>>(ResultEnum.SUCCESS,pageInfo);
    }


    @ApiOperation(value = "保存数据字典字典分类")
    @PostMapping("insertInfdictsort")
    @ApiImplicitParam(name="InfDictSort",value = "字典分类实体类")
    public Result<InfDictSort> insertInfdictsort(HttpServletRequest request, HttpServletResponse response , InfDictSort infDictSort){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        infDictSort.setCreator(loginId);
        infDictSortService.insertSelective(infDictSort);
        return new Result<InfDictSort>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "更新数据字典字典分类")
    @PostMapping("updateInfdictsort")
    @ApiImplicitParam(name="InfDictSort",value = "字典分类实体类")
    public Result<InfDictSort> updateInfdictsort(HttpServletRequest request, HttpServletResponse response, InfDictSort infDictSort){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        infDictSort.setModifiedBy(loginId);
        infDictSort.setModifyDate(new Date());
        infDictSortService.updateByIdSelective(infDictSort);
        return new Result<InfDictSort>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "获取所有的数据字典字典分类子目")
    @PostMapping("selectAllInfdictsort")
    public Result<List<InfDictSort>> selectAllInfdictsort(HttpServletRequest request, HttpServletResponse response){
        List<InfDictSort> list = new ArrayList<InfDictSort>();
        list = infDictSortService.selectAllInfdictsort();
        return new Result<List<InfDictSort>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据数据字典字典分类属性获取list")
    @PostMapping("selectInfdictsortByEntity")
    @ApiImplicitParam(name="InfDictSort",value = "字典分类实体类")
    public Result<InfDictSort> selectInfdictsortByEntity(HttpServletRequest request, HttpServletResponse response,InfDictSort infDictSort){
        List<InfDictSort> list = new ArrayList<InfDictSort>();
        list = infDictSortService.selectInfdictsort(infDictSort);
        return new Result<InfDictSort>(ResultEnum.SUCCESS,list.size());
    }

    @ApiOperation(value = "根据ids级联删除数据字典字典分类")
    @PostMapping("deleteInfdictsortByIds")
    //@ApiImplicitParams({@ApiImplicitParam(name="id", name="主键id"), @ApiImplicitParam(name="permission", name="当前按钮权限")})
    @ApiImplicitParam(name="InfDictSort",value = "字典分类实体类")
    public Result<InfDictSort> deleteInfdictsortByIds(HttpServletRequest request, HttpServletResponse response,String ids){
        List<InfDictSort> list = new ArrayList<InfDictSort>();
        int flag = infDictSortService.deleteByPrimaryKeys(ids);
        return new Result<InfDictSort>(ResultEnum.SUCCESS,null);
    }
}